﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Abp;
using Abp.Authorization;
using Abp.Dependency;
using Abp.Runtime.Session;

namespace Ny.CenterServers.Permissions
{
    /// <summary>
    /// 权限验证
    /// </summary>
    public class NyPermissionChecker: IPermissionChecker, ITransientDependency
    {
        /// <summary>
        /// session信息
        /// </summary>
        public IAbpSession AbpSession { get; set; }
        /// <summary>
        /// 验证
        /// </summary>
        /// <param name="permissionName">权限名称</param>
        /// <returns></returns>
        public Task<bool> IsGrantedAsync(string permissionName)
        {
            var userId = AbpSession.GetUserId();
            var tendid = AbpSession.TenantId;
            return IsGrantedAsync(new UserIdentifier(tendid, userId), permissionName);
        }
        /// <summary>
        /// 验证
        /// </summary>
        /// <param name="user">用户信息</param>
        /// <param name="permissionName">权限名称</param>
        /// <returns></returns>
        public Task<bool> IsGrantedAsync(UserIdentifier user, string permissionName)
        {
            var userId = user.UserId;
            var t = new Task<bool>(() =>
            {
                if (permissionName == SyPermissions.ActionTestCreate)
                {
                    return userId == 1;
                }
                return true;
            });
            t.Start();
            return t;
        }
    }
}
